package com.neuedu.service.impl;



import com.neuedu.dao.AddShoufeiNumberDao;
import com.neuedu.dao.ShouFeiDao;
import com.neuedu.dao.impl.AddShoufeiNumberDaoImpl;
import com.neuedu.dao.impl.ShouFeiDaoImpl;
import com.neuedu.model.AddShoufeiNumber;
import com.neuedu.model.ShouFei;
import com.neuedu.service.ShouFeiService;
import com.neuedu.vo.PageVO;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author dell
 */
public class ShouFeiServiceImpl implements ShouFeiService {
    private ShouFeiDao shouFeiDao=new ShouFeiDaoImpl();

    private AddShoufeiNumberDao addShoufeiNumberDao=new AddShoufeiNumberDaoImpl() {
    };

    @Override
    public PageVO<ShouFei> selectPage(String minDate, String maxDate, String likeName, Integer pageNo, Integer pageSize) {
        Integer limitParams1=(pageNo-1)*pageSize;
        Integer limitParams2=pageSize;
        List<ShouFei> shouFeis = shouFeiDao.selectPage(minDate, maxDate, likeName, limitParams1, limitParams2);
        PageVO<ShouFei> pageVO=new PageVO<>();
        pageVO.setPageNo(pageNo);
        pageVO.setPageSize(pageSize);
        pageVO.setData(shouFeis);
        //还差  pageCount页的总数  itemCount数据的总数
        Integer itemCount=shouFeiDao.count(minDate, maxDate, likeName);
        Integer pageCount=((itemCount%pageSize)==0)?(itemCount/pageSize):(itemCount/pageSize+1);
        pageVO.setItemCount(itemCount);
        pageVO.setPageCount(pageCount);
        return pageVO;
    }

    @Override
    public int addShouFei(ShouFei shouFei) {
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);

        AddShoufeiNumber addShoufeiNumber = addShoufeiNumberDao.select();
        String number="EP";
        number=number+format.replace("-","");
        if(addShoufeiNumber.getNumberTime().equals(format)){
            if(addShoufeiNumber.getNumber()<10){
                number=number+"00"+addShoufeiNumber.getNumber();
            }else if(addShoufeiNumber.getNumber()<100){
                number=number+"0"+addShoufeiNumber.getNumber();
            }else{
                number=number+addShoufeiNumber.getNumber();
            }
            addShoufeiNumber.setNumber(addShoufeiNumber.getNumber()+1);
            addShoufeiNumberDao.update(addShoufeiNumber);
        }else{
            addShoufeiNumber.setNumberTime(format);
            addShoufeiNumber.setNumber(1);
            addShoufeiNumberDao.update(addShoufeiNumber);
            number=number+"001";
        }
        shouFei.setShoufeibianhao(number);
        //处理创建日期
        sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format = sdf.format(date);
        shouFei.setChuangjianshijian(format);
        return shouFeiDao.insertShouFei(shouFei);
    }

    @Override
    public int delShouFei(Integer[] ids) {
        return shouFeiDao.deleteShouFei(ids);
    }

    @Override
    public ShouFei queryShouFei(Integer id) {
        return shouFeiDao.selectShouFeiById(id);
    }

    @Override
    public int saveShouFei(ShouFei shouFei) {
        return shouFeiDao.updateShouFei(shouFei);
    }


}
